package com.nulldev.util.net.dsap.packets;

import com.nulldev.util.net.dsap.Client;
import com.nulldev.util.net.dsap.Server;
import com.nulldev.util.net.packets.APILevel;
import com.nulldev.util.net.packets.NetInfo;
import com.nulldev.util.net.packets.PacketManager;
import com.nulldev.util.net.packets.UniversalPacket;
import com.nulldev.util.other.BooleanUtil;

@APILevel(level = 4)
public class DSAPEncryptedStreamsAllowed extends UniversalPacket {

	public DSAPEncryptedStreamsAllowed() {
		super("Dynamic Socket Allocation Protocol: Encrypted Stream Support Check", "dsap.check.encryption", "");
	}

	@Override
	public void client(byte[] data, byte[] raw, NetInfo client) {
		final boolean result = BooleanUtil.safeParse(new String(data));
		if (client.get() instanceof Client) {
			final Client cl = (Client) client.get();
			cl.setDSAPESSupport(result);
		}else {
			client.invoke("dsap.estream.supported", result);
		}
	}

	@Override
	public void server(byte[] data, byte[] raw, NetInfo connection) {
		if (connection.get() instanceof Server) {
			final Server s = (Server) connection.get();
			connection.reply(PacketManager.fromID("dsap.check.encryption", String.valueOf(s.supportsDSAP_SecureStreams())));
		}else {
			final boolean value = (boolean) connection.invoke("dsap.estream.supported");
			connection.reply(PacketManager.fromID("dsap.check.encryption", String.valueOf(value)));
		}
	}

}
